import { XObjectName } from '../../object';
import { RowObject } from '../../types';
import { Select } from '../query';
import { Statement, STATEMENT_KIND, STATEMENT_TAG } from '../statement';
import { CreateBehavior } from './types';

export class CreateView<
  T extends RowObject = any,
  N extends string = string
> extends Statement {
  static isCreateView(object: any): object is CreateView {
    return (
      Statement.isStatement(object) &&
      object.$kind === STATEMENT_KIND.CREATE_VIEW
    );
  }
  $kind: STATEMENT_KIND.CREATE_VIEW = STATEMENT_KIND.CREATE_VIEW;
  $name: XObjectName<N>;
  $body?: Select<T>;
  $behavior?: CreateBehavior;
  $tag: STATEMENT_TAG.EXECUTABLE = STATEMENT_TAG.EXECUTABLE;

  constructor(name: XObjectName<N>) {
    super();
    this.$name = name;
  }

  as(select: Select<T>): this {
    this.$body = select;
    return this;
  }
}

export function CreateViewBuilder<N extends string>(name: XObjectName<N>): CreateView {
  return new CreateView(name);
}

CreateViewBuilder.ifNotExists = function (name: XObjectName): CreateView {
  const statement = CreateViewBuilder(name);
  statement.$behavior = 'CREATE_IF_NOT_EXISTS';
  return statement;
};

// CreateViewBuilder.orReplace = function (name: XObjectName): CreateView {
//   const statement = CreateViewBuilder(name);
//   statement.$behavior = 'CREATE_OR_REPLACE';
//   return statement;
// };

export type CreateViewBuilder = typeof CreateViewBuilder;
